from flask import Blueprint
from flask import request
from flask import jsonify
import json
import datetime
import random
from database import db

bp_coupon =Blueprint('coupon',__name__)

@bp_coupon.route('/addc',methods=['POST','GET'])
def add():
    if request.method == "POST":
        #获取vue发过来的参加
        data = json.loads(request.data)
        print(data)
        code = datetime.datetime.strftime(datetime.datetime.now(),'%Y%m%d%H%M%S')+str(random.randint(10000,99999))
        total = int(data['count'])
        #用参数构造sql
        sql = "insert into coupon(code,man,jian,stime,etime,total) values('%s',%d,%d,'%s','%s',%d)"%(code,int(data['man']),int(data['jian']),data['stime'],data['etime'],total)
        #调用database里面的添加insert方法进行添加
        try:
             db.insert(sql)
             return jsonify({'code':200})
        except:
             return jsonify({'code':10010})
    else:
        return '获取'
